package graph.drawingshapes.shapes
{
	import common.interfaces.INameable;
	
	import flash.geom.Rectangle;
	
	import graph.drawingshapes.DrawingShapeBase;
	import graph.drawingshapes.IDragable;
	import graph.drawingshapes.Outline;
	import graph.drawingshapes.RedrawEvent;
	import graph.geometricshapes.*;
	
	import mx.events.PropertyChangeEvent;

	public class DrawingShape extends DrawingShapeBase implements INameable, IDragable
	{	
		public static const DefaultX:Number = 10;
		public static const DefaultY:Number = 10;
		public static const DefaultWidth:Number = 100;		
		public static const DefaultHeight:Number = 50;
		
		public static const NAME:String="DrawingShape";
		
		public virtual function get getName():String
		{
			return NAME;
		}	
		
		[Bindable]
		private var _outline:Outline = new Outline();
		[Bindable]
		public function get outline():Outline
		{
			return this._outline;
		}
		public function set outline(value:Outline):void
		{
			this._outline = value;
			this._outline.addEventListener(RedrawEvent.REDRAW, onRedrawEvent);
			dispatchEvent(new RedrawEvent());
		}
		
		[Bindable]
		private var _geometricShape : GeometricShape;
		[Bindable]
		public function get geometricShape():GeometricShape
		{
			return this._geometricShape;
		}
		public function set geometricShape(value:GeometricShape):void
		{
			this._geometricShape = value;
		}
		
		public var isDragged:Boolean = false;
		
		public function DrawingShape(x:Number=DefaultX, y:Number=DefaultY)
		{
			super();
			this.x = x;
			this.y = y;
			initHandlers();
		}
		
		protected override function initHandlers():void
		{
			super.initHandlers();
			this.outline.addEventListener(RedrawEvent.REDRAW, onRedrawEvent);
			this.outline.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onProperyChangeEvent);
		}
		
		protected virtual function onProperyChangeEvent(event:PropertyChangeEvent):void
		{
			dispatchEvent(event);
		}
				
		protected override function initDrawing():void
		{
			this.outline.beginDrawing(this.graphics);
		}
	
		
		[Bindable]
		public override function get x():Number
		{			
			return super.x;
		}
		public override function set x(value:Number):void
		{
			if (this.x != value)
			{
				var event:PropertyChangeEvent = PropertyChangeEvent.createUpdateEvent(this, "x", this.x, value);
				dispatchEvent(event);
				super.x = value;
			}
		}
		
		[Bindable]
		public override function get y():Number
		{			
			return super.y;
		}
		public override function set y(value:Number):void
		{
			if (this.y != value)
			{
				var event:PropertyChangeEvent = PropertyChangeEvent.createUpdateEvent(this, "y", this.y, value);
				dispatchEvent(event);
				super.y = value;
			}
		}
		
		[Bindable]
		public override function get width():Number
		{
			if (this.geometricShape == null)
			{
				return 0;
			}
			return this.geometricShape.width;
		}
		public override function set width(value:Number):void
		{
			if (this.geometricShape == null) return;
			if (this.width != value)
			{
				var event:PropertyChangeEvent = PropertyChangeEvent.createUpdateEvent(this, "width", this.width, value);
				dispatchEvent(event);
				this.geometricShape.width = value;
				dispatchEvent(new RedrawEvent());
			}
		}
		
		[Bindable]
		public override function get height():Number
		{
			if (this.geometricShape == null)
			{
				return 0;
			}
			return this.geometricShape.height;
		}
		public override function set height(value:Number):void
		{
			if (this.geometricShape == null) return;
			if (this.height != value)
			{
				var event:PropertyChangeEvent = PropertyChangeEvent.createUpdateEvent(this, "height", this.height, value);
				dispatchEvent(event);
				this.geometricShape.height = value;
				dispatchEvent(new RedrawEvent());
			}
		}
		
		public virtual function beginDrag(boundsRect:Rectangle = null):void
		{
			this.isDragged = true;
			this.startDrag(false, boundsRect);
		}
		
		public virtual function endDrag():void
		{
			this.isDragged = false;
			this.stopDrag();
		} 
	}
}